package com.qishao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qishao.model.EduTeacher;
import com.qishao.mapper.EduTeacherMapper;
import com.qishao.query.TeacherQuery;
import com.qishao.query.TeacherQuery_;
import com.qishao.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author xushaoqi
 * @since 2021-08-13
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Override
    public Map<String, Object> pageQuery(TeacherQuery teacher) {

        Page<EduTeacher> pageParam = new Page<>(teacher.getPage(), teacher.getSize());
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(teacher.getName())) {
            wrapper.like(TeacherQuery_.NAME, teacher.getName());
        }

        if (StringUtils.isNotBlank(teacher.getLevel())) {
            wrapper.eq(TeacherQuery_.LEVEL, teacher.getLevel());
        }

        if (StringUtils.isNotBlank(teacher.getStartTime())) {  //gt >
            wrapper.gt(TeacherQuery_.GMTCREATE, teacher.getStartTime());
        }

        if (StringUtils.isNotBlank(teacher.getEndTime())) { //lt <
            wrapper.lt(TeacherQuery_.GMTMODIFIED, teacher.getEndTime());
        }

        if (StringUtils.isNotBlank(teacher.getSort())) {
            wrapper.orderByAsc(TeacherQuery_.SORT, teacher.getSort());
        }
        //排序
        wrapper.orderByAsc(TeacherQuery_.SORT);

        baseMapper.selectPage(pageParam, wrapper);
        map.put("total", pageParam.getTotal());
        map.put("pages", pageParam.getPages());
        map.put("page", pageParam.getCurrent());
        map.put("size", pageParam.getSize());
        map.put("data", pageParam.getRecords());
        map.put("hasNest", pageParam.hasNext());
        map.put("hasPrevious", pageParam.hasPrevious());

        return map;
    }

    @Override
    public List<EduTeacher> findTeacherAll() {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
       wrapper.select("id","name");
        List<EduTeacher> eduTeachers = baseMapper.selectList(wrapper);
        return eduTeachers;
    }
}
